﻿package com.msjt.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.msjt.base.BaseAction;
import com.msjt.entity.NewJob;
import com.msjt.service.INewJobService;
import com.msjt.util.MyDate;
import com.msjt.util.PageMode;
/**
 * name:NewJobAction
 * function:招聘模块Action
 * author: kw
 * time:2013.8.28
 * updateTime:2013.8.29
 */
@SuppressWarnings("serial")
public class NewJobAction extends BaseAction {
	//driver Model
	private NewJob newJob;
	@Resource
	private INewJobService newJobService;
	//page Model
	private PageMode page;
	
	public PageMode getPage() {
		return page;
	}
	public void setPage(PageMode page) {
		this.page = page;
	}
	public NewJob getNewJob() {
		return newJob;
	}
	public void setNewJob(NewJob newJob) {
		this.newJob = newJob;
	}
	public INewJobService getNewJobService() {
		return newJobService;
	}
	public void setNewJobService(INewJobService newJobService) {
		this.newJobService = newJobService;
	}
	
	//添加招聘信息
	public String addNewJob() throws Exception{
		this.newJob.setJobDate(new MyDate().getDateString());
		boolean flag=this.newJobService.addNewJob(this.newJob);
		if(!flag){
			this.getRequest().put("msg", "<script>alert('添加失败！');history.go(-1);</script>");
			return "error";
		}
		return this.showJobM();
	}
	//删除招聘信息
	public String deleteNewJob() throws Exception{
		boolean flag=this.newJobService.deleteNewJob(this.newJob);
		if(!flag){
			this.getRequest().put("msg", "<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		return this.showJobM();
	}
	//准备修改招聘
	public String preUpdate() throws Exception{
		NewJob j=this.newJobService.getNewJobById(this.newJob);
		if(j==null){
			this.getRequest().put("msg", "<script>alert('该招聘不存在或已被删除，请刷新后重试！');history.go(-1);</script>");
			return "error";
		}
		this.getRequest().put("newJob", j);
		return "updateJob";
	}
	//修改招聘信息
	public String updateNewJob() throws Exception{
		System.out.println("我是修改");
		boolean flag=this.newJobService.updateNewJob(this.newJob);
		if(!flag){
			this.getRequest().put("msg", "<script>alert('修改失败！');history.go(-1);</script>");
			return "error";
		}
		return this.showJobM();
	}
	//分页显示招聘信息,前台显示招聘信息的全部
	public String showJob() throws Exception{
		if(this.page==null){
			this.page=new PageMode();
			this.page.setPageIndex(1);
		}
		this.page.setPageSize(5);
		this.page.setAction("newJob_showJob.html");
		this.page.setModelDrive("page");
		//设置总页数
		page.setSumRows(this.newJobService.getJobCount());
		//执行创建分页链接
		 page.createPageStr();
		List<NewJob> jobList=this.newJobService.getNewJobIndexPage(this.page.getPageIndex(), this.page.getPageSize());
		this.getRequest().put("jobList", jobList);
		this.getRequest().put("link",page.getPagingStr());
		 this.page=null;
		return "more";
	}
	//分页显示招聘信息，后台只显示招聘信息的职位,日期，截至日期
	public String showJobM() throws Exception{
		if(this.page==null){
			this.page=new PageMode();
			this.page.setPageIndex(1);
		}
		this.page.setPageSize(10);
		this.page.setAction("newJob_showJobM.html");
		this.page.setModelDrive("page");
		//设置总页数
		page.setSumRows(this.newJobService.getJobCount());
		//执行创建分页链接
		 page.createPageStr();
		List<NewJob> jobList=this.newJobService.getNewJobAfterPage(this.page.getPageIndex(), this.page.getPageSize());
		this.getRequest().put("jobList", jobList);
		this.getRequest().put("link",page.getPagingStr());
		 this.page=null;
		return "adminJob";
	}
	//批量删除
		public String deleteMore() throws Exception{
			String idString=ServletActionContext.getRequest().getParameter("idString");
			idString=idString.substring(0,idString.length()-1);
			boolean flag=this.newJobService.deleteMore(idString);
			if (!flag) {
				this.getRequest().put("msg",
						"<script>alert('删除失败！');history.go(-1);</script>");
				return "error";
			}
			return showJobM();
		}
	//得到详细信息
	public String getJob() throws Exception{
		NewJob n=this.newJobService.getNewJobById(this.newJob);
		if(n==null){
			this.getRequest().put("msg", "<script>alert('改招聘不存在或已被删除，请刷新后重试！');history.go(-1);</script>");
			return "error";
		}
		this.getRequest().put("newJob",n );
		return "jobDetail";
	}
}
